package com.atguigu.common.valid.validator;

import com.atguigu.common.valid.annotation.ListStringValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: Mr.ling
 * @data: 2022/7/9
 * @apiNote
 */
public class ListStringValueConstrainValidator implements ConstraintValidator<ListStringValue,String> {

    private Set<String> set = new HashSet<>();

    /**
     * 初始化
     * @param constraintAnnotation annotation instance for a given constraint declaration
     */
    @Override
    public void initialize(ListStringValue constraintAnnotation) {
        String[] args = constraintAnnotation.args();
        for (String arg : args) {
            set.add(arg);
        }
    }

    /**
     * 判断校验是否成功
     * @param value object to validate
     * @param context context in which the constraint is evaluated
     *
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
